package cn.com.simple.learning.designpattern.create.singleton;

/**
 * @date 2022/2/7
 * @description 单例模式
 */
public class SingletonPattern {

    // 构造方法私有化
    private SingletonPattern() {
    }

    /**
     * 1、懒汉式
     */
    private static final SingletonPattern singletonPattern = new SingletonPattern();

    public static SingletonPattern getInstance() {
        return singletonPattern;
    }

    /**
     * 2、饿汉式
     */
    private static volatile SingletonPattern hungryMan;

    public static SingletonPattern getInstance1() {
        if (hungryMan == null) {
            synchronized (SingletonPattern.class) {
                if (hungryMan == null) {
                    hungryMan = new SingletonPattern();
                    return hungryMan;
                }
            }
        }
        return hungryMan;
    }

    /**
     * 测试方法
     */
    public void test() {
        System.out.println("hello world!");
    }


}
